package com.headwind.logistics.permission.controller;

import com.headwind.logistics.common.util.LayuiUtil;
import com.headwind.logistics.common.util.RestResult;
import com.headwind.logistics.common.util.ResultGenerator;
import com.headwind.logistics.common.pojo.Menu;
import com.headwind.logistics.permission.service.intl.MenuServiceIntl;
import com.headwind.logistics.permission.service.intl.PermsServiceIntl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小恶魔
 * @Date 2020-07-19 17:27
 * @Version 1.0
 * @DESCRIPTION 按钮权限控制器
 */
@Controller
public class PermsController {
    //权限业务逻辑层接口
    @Resource
    private PermsServiceIntl permsServiceIntl;

    //api响应接口
    @Resource
    private ResultGenerator resultGenerator;

    //菜单业务逻辑层
    @Resource
    private MenuServiceIntl menuServiceIntl;

    /**
     * 获取按钮权限信息 分页
     * @return
     */
    @RequestMapping("/admin/queryPermsPage")
    @ResponseBody
    public LayuiUtil queryPermsPage(Integer page,Integer limit){

        List<Menu> menuList = permsServiceIntl.queryAllPermission(page, limit);

        return LayuiUtil.data(permsServiceIntl.queryAllPermission(null, null).size(),menuList);
    }

    /**
     * 添加按钮权限
     * @param menu
     * @param roleIdList
     * @return
     */
    @PostMapping("/admin/addPerms")
    @ResponseBody
    public RestResult addPerms(Menu menu,String roleIdList){

        Integer count = permsServiceIntl.addPerms(menu, roleIdList);

        if (count >0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("失败了哦,重试看看？");

    }

    /**
     * 修改按钮权限
     * @param menu
     * @return
     */
    @PostMapping("/admin/updatePerms")
    @ResponseBody
    public RestResult updatePerms(Menu menu){

        Integer count = permsServiceIntl.updatePermsById(menu);

        if (count >0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("操作失败，重新试试");

    }

    /**
     * 删除按钮权限
     * @param menuId
     * @return
     */
    @PostMapping("/admin/deletePerms/{menuId}")
    @ResponseBody
    public RestResult deletePerms(@PathVariable("menuId") Long menuId){

        Integer count = menuServiceIntl.deleteMenuByMenuId(menuId);

        if (count >0){
            return resultGenerator.getSuccessResult();
        }

        return resultGenerator.getFailResult("删除失败，重新试试？");
    }

}
